package com.example.demo.col.controller;

import com.example.demo.col.HelloService;
import com.example.demo.col.MySingletonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class Controller1 {
    @Resource
    HelloService helloService;
    @Resource
    MySingletonBean mySingletonBean ;
    @Autowired
    MySingletonBean mySingletonBean2;
    @Resource
    ApplicationContext applicationContext;

    @GetMapping("hello1")
    public String hello() {
        MySingletonBean bean1 = applicationContext.getBean(MySingletonBean.class);
        MySingletonBean bean2 = applicationContext.getBean(MySingletonBean.class);

        // 验证两个实例是否相等
        return String.format("Controller1 ID: %s\nBean2 ID: %s\nIs Same Instance: %b",
                bean1.getId(), mySingletonBean.getId(), mySingletonBean2 == bean2);
    }
}
